//
//  TouchFallLayer.cpp
//  slushy
//
//  Created by luotianqiang1 on 17/5/26.
//
//

#include "TouchFallLayer.h"
#include "FoodData.h"
#include "TouchNodeComponent.h"
#include "FrameStatus.h"
#include "particleLayer.h"
#include "MakeIce.h"
IMPLEMENT_COCOS2DX_CLASS(TouchFallLayer);
REGIST_SECENE(TouchFallLayer)
bool TouchFallLayer::init(){
    auto fullPath = FileUtils::getInstance()->fullPathForFilename("cutLayer/"+FoodData::getFlavor()+ ".csb");
    if(BaseStepLayer::init() && FileUtils::getInstance()->isFileExist(fullPath)) {
        _operate = StudioLayer::create(fullPath);
        addChild(_operate);
        _prompt = _operate->getNodeByName<Sprite*>("finger");
        _operate->getNodeByName("foodContainer")->setVisible(false);
        
        auto bowl = _operate->getNodeByName("bowl");
        CocosHelper::visitAllNode(bowl, [](Node* node){
            node->setVisible(node->getName() == "bowlUp");
        });
        touchCount = 0;
        return true;
    }
    return false;
}

void TouchFallLayer::touchEnd(ui::Widget* widget){
    
}


void TouchFallLayer::onEnterTransitionDidFinish(){
    BaseStepLayer::onEnterTransitionDidFinish();
    SoundPlayer::getInstance()->playEffect("sound/general/ingredients_fly_in.mp3");
    auto bowl = _operate->getNodeByName("bowl");
    ActionHelper::showBezier(bowl, bowl->getPosition(), ActionHelper::ShowDirection::show_from_left,[this](){
        _operate->getNodeByName("bowlShade")->runAction(FadeIn::create(0.2f));
    },0.9f);
    bowl->setVisible(true);
    
    ActionHelper::delayFunc(0.3f, this, [=](){
        SoundPlayer::getInstance()->playEffect("sound/general/ingredients_fly_in.mp3");
        auto foodContainer = _operate->getNodeByName("foodContainer");
        ActionHelper::showBezier(foodContainer, foodContainer->getPosition(), ActionHelper::ShowDirection::show_from_right,[=](){
            auto down  = _operate->getNodeByName("chick_2");
            _prompt->stopAllActions();
            _prompt->setPosition(_prompt->getParent()->convertToNodeSpace(_operate->getPosinWord(down->getName())));
            down->stopAllActions();
            _prompt->runAction(ActionHelper::createUpDownPrompt());
            _prompt->setVisible(true);
            down->setScale(0);
            down->runAction(RepeatForever::create(Sequence::create(DelayTime::create(0.3),ScaleTo::create(0.5, 1),DelayTime::create(0.1),ScaleTo::create(0, 0.3), nullptr)));
            down->setVisible(true);
            int touchNum = 0;
            for(auto child:foodContainer->getChildren())
                if(child->getName().find("food") != string::npos)
                    touchNum++;
            auto touchEvent = TouchNodeComponent::create();
            foodContainer->addComponent(touchEvent);
            touchEvent->addListners(ComponentTouchEnd, [=](LQComponent*,OperateListner*)  {
                SoundPlayer::getInstance()->playEffect("sound/add.mp3");
                
                touchEvent->setEnabled(false);
                stopPrompt();
                down->stopAllActions();
                down->setVisible(false);
                foodContainer->runAction(ActionHelper::createShakeAction());
                ActionHelper::delayFunc(0.2, this, [=](){
                    auto food = _operate->getNodeByName(StringUtils::format("food%d",touchCount));
                    if(food)
                        food->setVisible(false);
                    vector<Node*> inbowls;
                    for(auto child:bowl->getChildren()) {
                        if(child->getName().find(StringUtils::format("inbowl%d",touchCount)) != string::npos)
                            inbowls.push_back(child);
                    }
                    
                    for(int i=0;i<inbowls.size();i++){
                        auto f = FrameStatus::create(inbowls[i]->getName());
                        f->setSearchAction(_operate->getAction());
                        inbowls[i]->setVisible(true);
                        inbowls[i]->runAction(Sequence::create(DelayTime::create(0.2f*i), f->createAction(0.65f, "fallEnd"),CallFunc::create([=](){
                            if(i == inbowls.size()-1) {
                                 touchCount++;
                                if(touchCount <touchNum)
                                    touchEvent->setEnabled(true);
                                else {
                                    ActionHelper::hide(foodContainer, ActionHelper::ShowDirection::show_from_right);
                                    fallEnd();
                                }
                            }
                        }), nullptr));
                        f->remove();
                    }
                    
                   
                });
            });
            
        },0.9f);
        foodContainer->setVisible(true);
    });
}

void TouchFallLayer::fallEnd(){
    auto bowlShade = _operate->getNodeByName("bowlShade");
    bowlShade->setVisible(false);
    auto bowl = _operate->getNodeByName("bowl");
    bowl->runAction(Sequence::create(ScaleTo::create(0.9f, 1.25f),CallFunc::create([=](){
        this->addChild(particleLayer::create(),10);
    }),DelayTime::create(1.5),CallFunc::create([](){
        Director::getInstance()->replaceScene(TransitionPageTurn::create(1.2, MakeIce::createScene(), false));
    }), nullptr));
    bowl->runAction(JumpTo::create(0.9f, bowl->getParent()->getContentSize()*.5f, 200, 1));
}